from collections import OrderedDict
from bs4 import BeautifulSoup
import re

def resolve_rank_info(data):
    """
    解析每一页的所有基金的收益数据
    @param data:
    @return:
    """
    rank_pages_data = []
    data = data.replace("[", "").replace("]", "").replace("\"", "")
    for data_row in data.split(","):
        # 生成一个有序字典，保存排序结果
        rank_info = OrderedDict()
        row_arr = data_row.split("|")

        if len(row_arr)>16:
            # 获取每个星级的评分
            rank_info['基金代码'] = 'd'+row_arr[0]
            rank_info['基金名称'] = row_arr[1]
            rank_info['截止日期'] = row_arr[3]
            rank_info['单位净值'] = row_arr[4]
            rank_info['日增长率'] = row_arr[5]
            rank_info['近1周'] = row_arr[6]
            rank_info['近1月'] = row_arr[7]
            rank_info['近3月'] = row_arr[8]
            rank_info['近6月'] = row_arr[9]
            rank_info['近1年'] = row_arr[10]
            rank_info['近2年'] = row_arr[11]
            rank_info['近3年'] = row_arr[12]
            rank_info['今年来'] = row_arr[13]
            rank_info['成立来'] = row_arr[14]
            rank_info['起购金额'] = row_arr[-5]
            rank_info['原手续费'] = row_arr[-4]
            rank_info['现手续费'] = row_arr[-3]

            # 保存当前rank信息
            rank_pages_data.append(rank_info)

    return rank_pages_data


def resolve_rank_detail_info(fund_code, response):
    """
    解析基金的详细数据
    @param fund_code: 
    @param response:
    @return: 
    """
    rank_detail_info = OrderedDict()
    
    soup = BeautifulSoup(response.text, 'html.parser')
    rank_detail_info['基金代码'] = 'd'+fund_code
    soup_div = soup.find_all('div', class_='bs_gl')[0]
    rank_detail_info['成立日期'] = soup_div.find_all('label')[0].find_all('span')[0].get_text()
    rank_detail_info['基金经理'] = soup_div.find_all('label')[1].find_all('a')[0].get_text()
    rank_detail_info['类型'] = soup_div.find_all('label')[2].find_all('span')[0].get_text()
    rank_detail_info['管理人'] = soup_div.find_all('label')[3].find_all('a')[0].get_text()
    rank_detail_info['资产规模'] = soup_div.find_all('label')[4].find_all('span')[0].get_text().replace("\r\n", "").replace(" ", "")

    return rank_detail_info

def resolve_position_info(fund_code, text):
    """
    解析基金的持仓数据
    @param fund_code:
    @param text:
    @return:
    """
    fund_positions_data = []
    res_data = re.findall(r'\"(.*)\"', text)[0]
    soup = BeautifulSoup(res_data, 'html.parser')

    update_date = soup.find_all('label', class_='right lab2 xq505')[0].find_all('font')[0].get_text()
    soup_tbody = soup.find_all('table', class_='w782 comm tzxq')[0].find_all('tbody')[0]
    for soup_tr in soup_tbody.find_all('tr'):
        postion_info = OrderedDict()
        postion_info['基金代码'] = 'd'+fund_code
        postion_info['基金截止日期'] = soup.find_all('font', class_='px12')[0].get_text()

        postion_info['持仓排序'] = soup_tr.find_all('td')[0].get_text()
        postion_info['持仓股票代码'] = 'd'+soup_tr.find_all('td')[1].find_all('a')[0].get_text()
        postion_info['持仓股票名称'] = soup_tr.find_all('td')[2].find_all('a')[0].get_text()
        postion_info['持仓股票最新价'] = soup_tr.find_all('td')[3].find_all('span')[0].get_text()
        postion_info['持仓股票涨跌幅'] = soup_tr.find_all('td')[4].find_all('span')[0].get_text()
        postion_info['持仓股票占比'] = soup_tr.find_all('td')[6].get_text()
        postion_info['持仓股票持股数'] = soup_tr.find_all('td')[7].get_text()
        postion_info['持仓股票持股市值'] = soup_tr.find_all('td')[8].get_text().replace(",", "")
        postion_info['更新日期'] = update_date

        fund_positions_data.append(postion_info)

    return fund_positions_data

